<script type="text/javascript" language="javascript"> 

var map;
var markersArray = [];

    function setMarkers(map, locations) {
        for (var i = 0; i < locations.length; i++) {
            var producer = locations[i];
            var producerName = producer[0];
            var producerLatLng = new google.maps.LatLng(producer[1], producer[2]);
            var producerIcon = producer[3];
            var producerIndex = producer[5]; 
            var producerURL = producer[6];
            var producerInfo = producer[4];
            var producerGuid = producer[7];
            var producerCategories = producer[8];            
   
            var marker = new google.maps.Marker({
                position: producerLatLng,
                map: map,
                title: producerName,
                zIndex: producerIndex,
                producerInfo: producerInfo,
                producerURL: producerURL,
                producerGuid: producerGuid,
                producerCategories: producerCategories,
                clickable: true
            });

            markersArray.push(marker);

            var producerInfoWindow = new google.maps.InfoWindow({
                content: 'holding...'
            });

            google.maps.event.addListener(marker, 'clickable_changed', function(){
                window.open(this.producerURL, 'mywindow');
                });                
                

            google.maps.event.addListener(marker, 'click', function() {
              producerInfoWindow.setContent(this.producerInfo);
              //producerInfoWindow.setContent(marker.getZIndex());
              producerInfoWindow.open(map, this);
            });

 
        }
        
        return true;
    }

    function getMarkers(map, markers) {
        var companiesMapped = new Array();
        var k = 0;

        for (var i = 0; i < markers.length; i++) {
            var producer = markers[i];
            var producerLatLng = new google.maps.LatLng(producer[1], producer[2]);
            var mapBounds = new google.maps.LatLngBounds(producerLatLng, producerLatLng);
            if (map.getBounds()) {
                mapBounds = map.getBounds();
            }
            if (mapBounds.contains(producerLatLng)) {
                companiesMapped[k] = producer[7];
                k++;
            }
        }
    }

    // Create the map and put the all markers into it.
    function initialize(latlng, markers, container, global) {
        var myOptions = {
            zoom: 13,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }

       map = new google.maps.Map(document.getElementById(container), myOptions);

       setMarkers(map, markers);
         
       if (global == true) {
           
        	 google.maps.event.addListener(map, 'idle', function() {
                 getMarkers(this, markers);
            });                      
        }
        var form = document.getElementById('form_category_map');
        removeMarker(null, false, null, form);
        return map;

    }

    function removeMarker(value, checked, name, form){
        if (checked) {
        	setMapOnMarkers(value, false);            
        } else {          
            var categoriesChecked = [];
            var categoriesUnChecked = []; 
            var controlMarcados = 0;
            var controlDesmarcados = 0;      
            for (var f = 0; f < form.category.length; f++){
           	   if (form.category[f].checked){
          		  categoriesChecked[controlMarcados] = form.category[f].value;
        		   controlMarcados++;
        	   } 
        	    else {
            	   categoriesUnChecked[controlDesmarcados] = form.category[f].value;
            	   controlDesmarcados++;
        	    }
            }
            controllMarkers(categoriesUnChecked, true);
            controllMarkers(categoriesChecked, false);  
        }
    }       


    function setMapOnMarkers(value, setNull){
      	if (markersArray){
   	   	   for (var i = 0; i < markersArray.length; i++) {   
             if (in_array(value, markersArray[i].producerCategories)){
       	         if (setNull) {
       	        	 markersArray[i].setMap(null);
       	         } else markersArray[i].setMap(map);          	        	
       	     }	            
           }      
       }
    }
        
    
    function in_array(valor,vetor){
        for(var i in vetor){
            if(valor == vetor[i]){
                return true;
            }
        }
        return false;
    }

    function controllMarkers(categories, setNull){
    	for (var i = 0; i < categories.length; i++) {       
        	setMapOnMarkers(categories[i], setNull); 
        }       
    }      
    </script>